package cn.icare.cms.controller.restful;

import cn.icare.cms.NoticeKindService;
import cn.icare.cms.model.ro.AddNoticeKindRO;
import cn.icare.cms.model.ro.EditNoticeKindRO;
import cn.icare.cms.model.vo.rest.RespMsgVO;
import cn.icare.cms.model.vo.rest.UserVO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/rest/noticekind")
@Api(tags = "REST通知公告")
public class NoticeKindController extends BaseController {
    @Resource
    private NoticeKindService noticeKindService;
    private final String FALL_STRING = "请求失败,服务繁忙!";
    /**
     * URL:http://{ip}:{port}/rest/noticeKind
     * method:POST
     *
     * @param ro
     * @return
     */
    @PostMapping()
    @HystrixCommand(fallbackMethod = "createOneFall")

    @ApiOperation(value = "新增单个公告类型", notes = "t_noticeKind表新增单个公告类型")
    public ResponseEntity<RespMsgVO> createOne(@RequestBody AddNoticeKindRO ro) {

        Integer id = this.noticeKindService.createOne(ro);
        if (id >= 0) {
            return super.getRespOk(id);
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/noticeKind
     * method:PUT
     *
     * @param ro
     * @return
     */
    @PutMapping()
    @HystrixCommand(fallbackMethod = "updateOneFall")
    @ApiOperation(value = "更新单个公告类型", notes = "t_noticeKind表更新单个公告类型")
    public ResponseEntity<RespMsgVO> updateOne(@RequestBody EditNoticeKindRO ro) {
        if (this.noticeKindService.updateOne(ro)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/noticeKind/{id}
     * method:DELETE
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @HystrixCommand(fallbackMethod = "deleteOneFall")
    @ApiOperation(value = "删除单个公告类型", notes = "t_noticeKind表删除符合主键的单个公告类型")
    public ResponseEntity<RespMsgVO> deleteOne(@PathVariable("id") Integer id) {
        if (this.noticeKindService.deleteOne(id)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/noticeKind?ids={ids}
     * method:DELETE
     *
     * @param ids
     * @return
     */
    @DeleteMapping()
    @HystrixCommand(fallbackMethod = "deleteBatchFall")
    @ApiOperation(value = "删除多个公告类型", notes = "t_noticeKind表删除符合主键的多个公告类型")
    public ResponseEntity<RespMsgVO> deleteBatch(@RequestParam("ids") List<Integer> ids) {
        if (this.noticeKindService.deleteBatch(ids)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/noticeKind/{id}
     * method:GET
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @HystrixCommand(fallbackMethod = "getOneFall")
    @ApiOperation(value = "查询单个公告类型", notes = "t_noticeKind表返回符合主键的单个公告类型")
    public ResponseEntity<RespMsgVO> getOne(@PathVariable("id") Integer id) {
        return super.getRespOk(this.noticeKindService.getOne(id));
    }

    /**
     * URL:http://{ip}:{port}/rest/noticeKind?args={args}
     * method:GET
     *
     * @return
     */
    @GetMapping()
    @HystrixCommand(fallbackMethod = "getListFall")
    @ApiOperation(value = "查询多个公告类型", notes = "返回符合条件的多个公告类型")
    public ResponseEntity<RespMsgVO> getList(@RequestParam(name = "name", required = false) String name
                                             ) {


            return super.getRespOk(this.noticeKindService.getList(name));

    }



    //#region fallback methods
    private ResponseEntity<RespMsgVO> createOneFall(AddNoticeKindRO ro) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> updateOneFall(EditNoticeKindRO ro) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> deleteOneFall(Integer id) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> deleteBatchFall(List<Integer> ids) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getOneFall(Integer id) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getListFall(String name) {
        return super.getRespFail(this.FALL_STRING);
    }
    //#endregion

}
